package SampleService;

import support.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

/**
 * DataPingServlet allows you to test your HTTP POST/GET operations. Whatever data you send it, is sent back, so you
 * can see progress monitors working in reporting Send/Recieve updates.
 *
 * @author Nazmul Idris
 * @version 1.0
 * @since Mar 28, 2008, 7:06:21 PM
 */
public class DataPingServlet extends HttpServlet {

@Override protected void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
{


  ServletOutputStream sos = res.getOutputStream();

  res.setContentType(MIMETypeConstantsIF.PLAIN_TEXT_TYPE);
  sos.write("This is the ping servlet".getBytes());

  sos.flush();
  sos.close();

}

@Override protected void doPost(HttpServletRequest request, HttpServletResponse res)
    throws ServletException, IOException
{

  ByteBuffer inputBB = new ByteBuffer(request.getInputStream());
  ByteBuffer outputBB = null;

  try {

    // extract the hashmap
    System.out.println("trying to extract hashtable from request");

    ObjectInputStream ois = new ObjectInputStream(inputBB.getInputStream());
    Hashtable<String, String> input = (Hashtable<String, String>) ois.readObject();
    System.out.println("got the uid/pwd from the client:" + input);

    Object retval = _processInput(input);
    System.out.println("created response hashtable, sending it back");

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(retval);

    outputBB = new ByteBuffer(baos.toByteArray());

    System.out.println("sent response back...");

  }
  catch (Exception e) {
    System.out.println(e);
    e.printStackTrace();
  }

  ServletOutputStream sos = res.getOutputStream();

  if (outputBB != null) {
    res.setContentType("application/octet-stream");
    res.setContentLength(outputBB.getSize());
    sos.write(outputBB.getBytes());
  }
  else {
    res.setContentType("application/octet-stream");
    res.setContentLength(inputBB.getSize());
    sos.write(inputBB.getBytes());
  }

  sos.flush();
  sos.close();

}

/** actually get user profile object from service and send it back */
private Hashtable<DataKeys, Serializable> _processInput(Hashtable<String, String> input) {

  Hashtable<DataKeys, Serializable> retval = new Hashtable<DataKeys, Serializable>();

  retval.put(DataKeys.str1, input.get("uid"));
  retval.put(DataKeys.str2, input.get("pwd"));

  try {
    InputStream is = getClass().getClassLoader().getResourceAsStream("information.png");
    if (is == null) System.out.println("is is null!");
    ByteBuffer bb = new ByteBuffer(is);
    retval.put(DataKeys.img1, bb.getBytes());
  }
  catch (IOException e) {
    System.out.println(e);
    e.printStackTrace();
  }

  return retval;

}

}//end class DataPingServlet
